﻿namespace zijian666.Data.SqlBuilders;
internal class SqliteBuilder : TSqlBuilder
{
    public static readonly SqliteBuilder Instance = new SqliteBuilder();
    public override string PagingSql(string sql, long begin, int count)
    {
        return $"{sql.TrimSemicolon()} LIMIT {count} OFFSET {begin}";
    }

    private DbProviderFactory _dbProvider = default!;

    public override DbProviderFactory DbProvider
    {
        get
        {
            if (_dbProvider is null)
            {
                if (DbProviderFactories.TryGetFactory("System.Data.SQLite", out _dbProvider!))
                {
                    return _dbProvider;
                }
                var name = DbProviderFactories.GetProviderInvariantNames().FirstOrDefault(x => x.Contains("SQLite", StringComparison.OrdinalIgnoreCase));
                if (name is null)
                {
                    throw new NotSupportedException("未找到 SQLite 提供程序");
                }
                _dbProvider = DbProviderFactories.GetFactory(name);
            }
            return _dbProvider;
        }
    }

    public override string GetParameterHolder(string parameterName)
    {
        return $"${parameterName}";
    }
}
